package com.xxx.demo.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xxx.demo.entity.CustomerEntity;
import com.xxx.demo.entity.News;
import com.xxx.demo.service.NewsService;
import com.xxx.demo.vo.NewsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;


/**
 * @author HT
 * @date 2023/3/20
 * @apiNote
 */
@Controller
@RequestMapping("/manage")
public class NewsController {

    @Autowired
    private NewsService newsService;

    /**
     * 分页查询新闻
     * @param pageNo
     * @param pageSize
     * @param news
     * @param model
     * @return
     */
    @RequestMapping("/queryNews")
    public String queryNews(Integer pageNo, Integer pageSize, @ModelAttribute("news") News news, Model model){
        if (pageNo == null){
            pageNo = 1;
        }
        if (pageSize == null){
            pageSize = 3;
        }
        IPage<News> newsIPage = newsService.selectNewsPage(pageNo, pageSize, news);
        IPage<NewsVo> newsVoPage = new Page<>();
        List<News> records = newsIPage.getRecords();
        List<NewsVo> newsVoList = new ArrayList<>();
        BeanUtil.copyProperties(newsIPage,newsVoPage);
        if (CollectionUtil.isEmpty(records)){
            return "/manage/news";
        }else {
            for(News date:records){
                NewsVo newsVo = new NewsVo();
                BeanUtil.copyProperties(date,newsVo);
                newsVo.setStrId(date.getId() + "!");
                newsVoList.add(newsVo);
            }
            newsVoPage.setRecords(newsVoList);
        }
        model.addAttribute("newsPage",newsVoPage);
        return "/manage/news";
    }



    /**
     * 新增新闻
     * @param news
     * @return
     */
    @RequestMapping("/insertNews")
//    @ResponseBody
    public String insertNews(@ModelAttribute News news){
        int i = newsService.saveNews(news);
        System.out.println(news);
//        if (i == 0){
//            return "0";
//        }
        return "redirect:selectNewsPage";
    }



    /**
     * 根据Id删除新闻（逻辑删除）
     * @param id
     * @return
     */
    @RequestMapping("/deleteNews")
    @ResponseBody
    public String deleteNews(String id){
        id = id.replace("!","");
        System.out.println(id);
        Long aLong = Long.valueOf(id);
        newsService.deleteNews(aLong);
        return "1";
    }

    /**
     * 根据id查询新闻
     * @param id
     * @param model
     * @return
     */
    @RequestMapping("/queryNewsById")
    public String queryNewsById(Long id,Model model){
        News news = newsService.queryNewsById(id);
        model.addAttribute("news",news);
        return "manage/news-update";
    }



    /**
     * 修改新闻
     * @param news
     * @return
     */
    @RequestMapping("/updateNews")
    @ResponseBody
    public String updateNews(@ModelAttribute News news){
        newsService.updateNews(news);
        return "1";
    }

    @RequestMapping("/selectNewsPage")
    public String selectNewsPage(Integer pageNo, Integer pageSize, @ModelAttribute("news") News news, Model model){
       if (pageNo == null){
           pageNo = 1;
       }
       if (pageSize == null){
           pageSize = 3;
       }
        IPage<News> newsIPage = newsService.selectNewsPage(pageNo, pageSize, news);
        IPage<NewsVo> newsVoPage = new Page<>();
        List<News> records = newsIPage.getRecords();
        List<NewsVo> newsVoList = new ArrayList<>();
        BeanUtil.copyProperties(newsIPage,newsVoPage);
        if (CollectionUtil.isEmpty(records)){
            return "/manage/news";
        }else {
            for(News date:records){
                 NewsVo newsVo = new NewsVo();
                 BeanUtil.copyProperties(date,newsVo);
                 newsVo.setStrId(date.getId() + "!");
                 newsVoList.add(newsVo);
            }
            newsVoPage.setRecords(newsVoList);
        }
        model.addAttribute("newsPage",newsVoPage);
        return "/manage/news";
    }
    @RequestMapping("/selectNewsById")
    public String selectNewsById(Long id, Model model, HttpSession session){
        CustomerEntity customerEntity = (CustomerEntity) session.getAttribute("customer");
        if (ObjectUtil.isEmpty(customerEntity)){
            String error = "请先进行登录！！！";
            model.addAttribute("error",error);
            return "index";
        }
        News news = newsService.queryNewsById(id);
        model.addAttribute("news",news);
        model.addAttribute("newsList", newsService.selectAllNews());
        model.addAttribute("customerSession",session.getAttribute("customer"));
        return "news-view";
    }

}
